package com.pap.rbac.user.controller;

import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.util.date.DateUtils;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.base.util.string.StringUtilss;
import com.pap.obj.vo.business.DeleteFlagEnum;
import com.pap.obj.vo.business.DisableFlagEnum;
import com.pap.obj.vo.response.ResponseVO;
import com.pap.rbac.agent.user.IUserAgent;
import com.pap.rbac.user.auto.entity.User;

@RestController
@RequestMapping("/v1/rbac/user")
public class UserController extends BaseController<User, IUserAgent> {

	private static Logger logger  =  LoggerFactory.getLogger(UserController.class);
	
	@Resource(name = "userAgent")
	@Override
	public void setService(IUserAgent agent) {
		super.service = agent;
	}
	
	@Override
	@RequestMapping(value = "/add", method = {RequestMethod.POST})
	public ResponseVO<User> add(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "addJSON", required = true) String addJSON) throws Exception {
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		User e = objectMapper.readValue(addJSON, User.class);
		if(StringUtilss.isEmpty(e.getId())) {
			e.setId(UUID.randomUUID().toString());
		}
		e.setLoginName(e.getName());
		e.setDeleteFlag(DeleteFlagEnum.NO.getKey());
		e.setDisableFlag(DisableFlagEnum.NO.getKey());
		e.setCreateIp("0.0.0.0");
		e.setCreateTime(DateUtils.getCurrDateTimeStr());
		e.setCreateUser(loginUserId);
		e.setClientLicenseId(clientLicenseId);

		int operationInt = service.insertSelective(e);
		return ResponseVO.successdata(operationInt);
	}
	
	@RequestMapping(value = "/selectuserbyid", method = {RequestMethod.GET})
	public String selectUserById(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "id", required = true) String id) throws Exception {
		User user = service.selectByPrimaryKey(id);
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		return objectMapper.writeValueAsString(user);
	}
	
}
